<?php

/**
 * Yii Controller ProductController
 *
 * @author trongkhoi
 * @since Nov 29, 2014 - 11:03:43 PM
 */
class ProductController extends CrudController
{

    public $modelName = 'Product';
    public $title = 'Product';
    public $formView = '_form';

    public function init()
    {
        parent::init();
        $this->indexColumns = array(
            array(
                'class' => 'CCheckBoxColumn',
                'id' => 'ids',
                'selectableRows' => 2,
            ),
            array(
                'header' => Yii::t('user', 'Name'),
                'name' => 'name',
                'type' => 'raw',
                'value' => 'CHtml::link($data->name,$data->editUrl,array("target"=>"blank"))',
//                'filter' => CHtml::textField('name', getQuery('name', ''), array('placeHolder' => Yii::t('user', 'Search'))),
            ),
//            array(
//                'header' => Yii::t('user', 'Category'),
//                'name' => 'category_id',
//                'type' => 'raw',
//                'value' => 'CHtml::link(@$data->category->name,@$data->category->editUrl,array("target"=>"blank"))',
//                'filter' => CHtml::dropDownList("category_id", getQuery('category_id', 0), Category::getPublicArray(), array(
//                    "data-rel" => "chosen"
//                )),
//            ),
            array(
                'header' => Yii::t('user', 'Status'),
                'name' => 'status',
                'type' => 'raw',
                'value' => '$data->statusLabel',
                'filter' => false
            ),
            array('class' => 'application.widgets.MyButtonColumn',
                'editUrl' => '$data->editUrl',
                'deleteUrl' => '#',
                'htmlOptions' => array(
                    'width' => '1%'
                )
            ),
        );

        $this->formColumns = array(
            'name' => array(
                'type' => 'createUrl',
                'for' => 'url'
            ),
            'url',
            'category_id' => array(
                'type' => 'dropDownList',
                'data' => Category::getPublicArray(),
            ),
            'info_technical' => array(
                'type' => 'ckeditor',
            ),
            'summary' => array(
                'type' => 'textArea',
            ),
//            'description' => array(
//                'type' => 'ckeditor',
//            ),
            'picture' => array(
                'type' => 'ckfinder',
                'alt' => 'ckfinder',
                'data' => $this->model->getPublicImg()
            ),
            'status' => array(
                'type' => 'radioButtonList',
                'data' => Product::getArrayStatus(),
            ),
        );
    }

    protected function save($action = 'Create')
    {
        if (parent::save($action))
        {
            $this->_uploadGallery($this->model->id);
            $this->_deletedGallery();
        }
    }

    protected function _uploadGallery($pid)
    {
        $dir = 'productAlbum';
        $fileKey = 'galleryImage';
        if (@$_FILES[$fileKey])
        {
            foreach ($_FILES[$fileKey]['name'] as $fkey => $fname)
            {
                // Lấy extension của file và tạo tên file mới
                $ext = pathinfo($fname, PATHINFO_EXTENSION);
                $image_file = "files/$dir/" . md5(time() . $fname) . ".$ext";
                $thumb_file = "files/$dir/" . md5(time() . $fname) . "_.$ext";

                // Move file vào thư mục cần lưu                
                if (@move_uploaded_file($_FILES[$fileKey]['tmp_name'][$fkey], $image_file))
                {
                    // Resize ảnh
                    $thumb = Yii::app()->phpThumb->create($image_file);
                    $thumb->save($thumb_file);

                    // Lưu vào csdl
                    $si = new ProductImage();
                    $si->file = $image_file;
                    $si->product_id = $pid;
//                    $si->is_default = BusinessGallery::IS_DEFAULT;
                    $si->save();
                }
            }
            return TRUE;
        }
    }

    protected function _deletedGallery()
    {
        if (isset($_POST['deleteGallery']))
        {
            foreach ($_POST['deleteGallery'] as $galleryId)
            {
                $gallery = ProductImage::model()->findByPk($galleryId);
                if ($gallery != null)
                {
                    @unlink($gallery->file);
                    ProductImage::model()->deleteByPk($galleryId);
                }
            }
        }

        return TRUE;
    }
}

/** 
 * End file ProductController
 */
